<template>
  <div class="edit-popup">
    <popup
      ref="popupRef"
      :title="popupTitle"
      :async="true"
      width="550px"
      @confirm="handleSubmit"
      @close="handleClose"
    >
      <el-form
        ref="formRef"
        :model="formData"
        label-width="84px"
        :rules="formRules"
      >
        <el-form-item label="大厦编号" prop="sn">
          <el-input
            disabled
            placeholder="请重新打开生成编号"
            v-model="formData.sn"
            clearable
            :maxlength="100"
          />
        </el-form-item>
        <el-form-item label="大厦名称" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输入大厦名称"
            clearable
            :maxlength="100"
          />
        </el-form-item>
        <el-form-item label="总楼层" prop="totalFloor">
          <el-input
            type="number"
            v-model="formData.totalFloor"
            placeholder="请输入大厦总楼层"
            clearable
          />
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input
            v-model="formData.address"
            placeholder="请输入详细地址"
            clearable
            :maxlength="30"
          />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input
            v-model="formData.longitude"
            placeholder="请输入经度"
            clearable
            :maxlength="30"
          />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input
            v-model="formData.latitude"
            placeholder="请输入纬度"
            clearable
            :maxlength="30"
          />
        </el-form-item>
        <el-form-item label="送达时间" prop="deliveryTime">
          <el-input
            v-model="formData.deliveryTime"
            placeholder="请输入送达时间"
            clearable
            :maxlength="30"
          />
          <!-- <el-time-picker
            v-model="formData.deliveryTime"
            format="HH:mm"
            placeholder="请选择"
          >
          </el-time-picker> -->
        </el-form-item>
      </el-form>
    </popup>
  </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from "element-plus";
import Popup from "@/components/popup/index.vue";
import { useDictOptions } from "@/hooks/useDictOptions";
import feedback from "@/utils/feedback";
import { addMansion, editMansion, findMansionDetail } from "@/api/admin/index";

/* const props = defineProps({
    sn: {
        type: Number,
    },
}); */
const emit = defineEmits(["success", "close"]);
const formRef = shallowRef<FormInstance>();
const popupRef = shallowRef<InstanceType<typeof Popup>>();
const mode = ref("add");
const popupTitle = computed(() => {
  return mode.value == "edit" ? "编辑" : "新建";
});
const formData = reactive({
  id: "",
  sn: "" as string | number,
  name: "",
  totalFloor: 0 as number,
  address: "",
  longitude: "",
  latitude: "",
  deliveryTime: "11:40",
});
const formRules = {
  name: [
    {
      required: true,
      message: "请输入大厦名称",
      trigger: ["blur"],
    },
  ],
  totalFloor: [
    {
      required: true,
      message: "请输入总楼层",
      trigger: ["blur"],
    },
  ],
  address: [
    {
      required: true,
      message: "请输入大厦名称",
      trigger: ["blur"],
    },
  ],
  longitude: [
    {
      required: true,
      message: "请输入经度",
      trigger: ["blur"],
    },
  ],
  latitude: [
    {
      required: true,
      message: "请输入纬度",
      trigger: ["blur"],
    },
  ],
  deliveryTime: [
    {
      required: true,
      message: "请输入送达时间",
      trigger: ["change"],
    },
  ],
};

const handleSubmit = async () => {
  await formRef.value?.validate();
  mode.value == "edit"
    ? await editMansion(formData)
    : await addMansion(formData);
  popupRef.value?.close();
  feedback.msgSuccess("操作成功");
  emit("success");
};

const open = (type = "add") => {
  mode.value = type;
  popupRef.value?.open();
};

const setFormData = (data: Record<any, any>) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      //@ts-ignore
      formData[key] = data[key];
    }
  }
};

const getDetail = async (row: Record<string, any>) => {
  const data = await findMansionDetail({
    id: row.id,
  });
  setFormData(data);
};

const handleClose = () => {
  emit("close");
};

defineExpose({
  open,
  setFormData,
  getDetail,
});
</script>
